package yanyan.com.tochar;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;

import com.baidu.mobads.AppActivity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import yanyan.com.tochar.db.MyMediaRecorder;
import yanyan.com.tochar.utils.CoreUtil;
import yanyan.com.tochar.utils.HttpUtil;
import yanyan.com.tochar.utils.ProgressDiaolog;
import yanyan.com.tochar.utils.StringUtil;
import yanyan.com.tochar.utils.ToastUtil;

/**
 * \藏头诗
 */
public class FunsOrzPoetryActivity extends AppActivity {
    private ArrayAdapter<String> typeAdapter,numAdpter;
    private ProgressDialog loadin;

    private Spinner typeSpinner,numSpinner;
    private EditText centxetEd,centextRes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_funs_poetry);
        centxetEd=findViewById(R.id.poetry_context);
        typeSpinner=findViewById(R.id.poetry_type);
        numSpinner=findViewById(R.id.poetry_num);
        centextRes=findViewById(R.id.poetry_res);


        List<String> typeList=new ArrayList();
        typeList.add("藏头");
        typeList.add("藏尾");
        typeAdapter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,typeList);
        typeAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        typeSpinner.setAdapter(typeAdapter);
        List<String>numList=new ArrayList<>();
        numList.add("五言");
        numList.add("七言");
        numAdpter=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,numList);
        numAdpter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        numSpinner.setAdapter(numAdpter);

        //统计各功能
        CoreUtil.statisticalFuns(this,"funs_orz_poetry");
    }

    public void getPoetry(View view){
       String centext= centxetEd.getText().toString();
        if(centext.length()<3){
            ToastUtil.showLongToast(this,"输入字数太少");
            return;
        }
        loadin=new ProgressDialog(this);
        loadin.setMessage("正在生成...");
        loadin.show();
        String zishu="5";
        if(numSpinner.getSelectedItemPosition()!=0){
            zishu="7";
        }
        String type="0";
        if(typeSpinner.getSelectedItemPosition()!=0){
            type="2";
        }



        String url="http://www.shicimingju.com/cangtoushi/index.html?kw="+centext+"&zishu="+zishu+"&position="+type;
        Call msg = HttpUtil.getMsg(url);
        msg.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
               loadin.cancel();
                ToastUtil.showLongToast(FunsOrzPoetryActivity.this,"出错了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                loadin.cancel();
                String res=response.body().string();
                res=res.substring(res.indexOf("</form>"));
                List<String> resList= StringUtil.getMiddleStrList(res,"<div class=\"card\" style=\"padding: 10px\">","</div>");

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        String finalRes="";
                        for (String s:resList){
                            s=s.replace("<br>","");
                            finalRes=finalRes+s+"\n\n";
                        }
                        if(finalRes.length()<10){
                            finalRes="没有生成藏头诗,换行字试试";
                        }
                        centextRes.setText(finalRes);
                    }
                });

            }
        });
    }
}
